-
Notifications
You must be signed in to change notification settings - Fork 202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IF-STRIDE-STAKEIBC-UPDATEDELEGATIONBALANCES fix #447
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…com:Stride-Labs/stride into IF-STRIDE-STAKEIBC-UPDATEDELEGATIONBALANCES
sampocs
reviewed
Dec 3, 2022
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lgtm!
sampocs
approved these changes
Dec 3, 2022
sontrinh16
pushed a commit
to notional-labs/stride
that referenced
this pull request
Mar 27, 2023
Underflow possible when decreasing staked balance for host zone # Involved artifacts - [x/stakeibc/keeper/icacallbacks_undelegate.go](https://github.com/Stride-Labs/stride/blob/v2.0.3/x/stakeibc/keeper/icacallbacks_undelegate.go#L128) # Description When undelegating from each validator, host zone's staked balance is being decreased according to undelegation amount of particular validator: ```go for _, undelegation := range undelegateCallback.SplitDelegations { undelegateAmt, err := cast.ToInt64E(undelegation.Amount) k.Logger(ctx).Info(fmt.Sprintf("UndelegateCallback, Undelegation: %d, validator: %s", undelegateAmt, undelegation.Validator)) if err != nil { errMsg := fmt.Sprintf("Could not convert undelegate amount to int64 in undelegation callback | %s", err.Error()) k.Logger(ctx).Error(errMsg) return sdkerrors.Wrapf(types.ErrIntCast, errMsg) } undelegateVal := undelegation.Validator success := k.AddDelegationToValidator(ctx, zone, undelegateVal, -undelegateAmt) if !success { return sdkerrors.Wrapf(types.ErrValidatorDelegationChg, "Failed to remove delegation to validator") } zone.StakedBal -= undelegation.Amount } ``` However, `zone.StakedBal` is defined as `uint64`; therefore, it might underflow if `undelegation.Amount` > `zone.StakedBal`. Undelegation amount for each undelegation is calculated in [GetTargetValAmtsForHostZone](https://github.com/Stride-Labs/stride/blob/v2.0.3/x/stakeibc/keeper/unbonding_records.go#L74) and have been additionaly modified during [overflow](https://github.com/Stride-Labs/stride/blob/v2.0.3/x/stakeibc/keeper/unbonding_records.go#L103-L139) handling. # Problem Scenarios If underflow happens, staked balance of particular zone will unexpectedly and quietly become tremendously large having adverse consequences during both, `UndelegateCallback` and `DelegateCallback`. # Recommendation Add validation check before decreasing the staked balance amount: ```go if undelegation.Amount > zone.StakedBal { // handle incoming underflow } else { zone.StakedBal -= undelegation.Amount } ```
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Underflow possible when decreasing staked balance for host zone
Involved artifacts
Description
When undelegating from each validator, host zone's staked balance is being decreased according to undelegation amount of particular validator:
However,
zone.StakedBal
is defined asuint64
; therefore, it might underflow ifundelegation.Amount
>zone.StakedBal
. Undelegation amount for each undelegation is calculated in GetTargetValAmtsForHostZone and have been additionaly modified during overflow handling.Problem Scenarios
If underflow happens, staked balance of particular zone will unexpectedly and quietly become tremendously large having adverse consequences during both,
UndelegateCallback
andDelegateCallback
.Recommendation
Add validation check before decreasing the staked balance amount: